import Enemy from 'Enemy.js'
import Bullet from 'Bullet.js';

class Pool {
    constructor(ctx) {
        this.ctx = ctx;
        this.enemys = [];
        this.bullets = [];
        this.top = 0;
    }
    // 批量绘制敌机
    drawEnemys() {
        this.top += 1;
        //垂直方向每移动40个像素，就产生一架敌机
        if (this.top % 100 == 0) {
            const enemy = new Enemy(this.ctx);
            this.enemys.push(enemy)
        }
        //过滤不需要显示的敌机
        this.enemys = this.enemys.filter(enemy => {
            return enemy.status;
        })

        // for (let i in this.enemys) {
        //     if (this.enemys[i].status == false) {
        //         delete this.enemys[i]
        //     }
        // }
        // console.log(this.enemys.length);

        //绘制敌机
        this.enemys.forEach(enemy => {
            enemy.draw();
        })
    }

    drawHeroBullet(hero) {
        //垂直方向每移动40个像素，就产生一架敌机
        if (this.top % 30 == 0) {
            const bullet = new Bullet(this.ctx, hero);
            this.bullets.push(bullet)
        }
        //过滤不需要显示的敌机
        this.bullets = this.bullets.filter(bullet => {
            return bullet.status;
        })
        //绘制敌机
        this.bullets.forEach(bullet => {
            bullet.draw();
        })
    }
}

export default Pool;